package org.example.login.Impl;


import org.example.login.service.UitlsService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;


@Service
public class UitlsServiceImpl implements UitlsService {

    private String appId = "wx970a984c3506109f";
    private String secret = "77d7148a4af19e75198193be02167c9b";

    private UitlsServiceImpl() {}
    private static UitlsServiceImpl instance;

    public static UitlsServiceImpl getInstance() {
        if (instance == null) {
            synchronized (UitlsServiceImpl.class) {
                if (instance == null) {
                    instance = new UitlsServiceImpl();
                }
            }
        }
        return instance;
    }

    public JSONObject getWxOpenid(String code){
        System.out.println(code);
        // 继续获取openid
        WebClient client = WebClient.create();
        String url = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appId, secret, code);
        String response = client.get()
                .uri(url)
                .retrieve()
                .bodyToMono(String.class)
                .block();
        System.out.println(response);
        JSONObject jsonObject = JSONObject.parseObject(response);
        System.out.println(jsonObject);
        return jsonObject;
    }
}
